package com.micro.common.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @Classname DateUtil
 * @Description Date时间工具类
 * @Date 2019/6/25 10:57
 */
public class DateUtil {
    
    private static final Logger LOGGER = Logger.getLogger(DateUtil.class.getSimpleName());

    //年 月 日 时 分 秒
    public static final String YEAR_MONTH_DAY_TIME_MINUTE_SECONDS = "yyyy-MM-dd HH:mm:ss";

    //年 月 日
    public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";

    //时 分 秒
    public static final String TIME_MINUTE_SECONDS = "HH:mm:ss";

    //分 秒
    public static final String MINUTE_SECONDS = "mm:ss";

    /**
     * 计算两个时间相差的天数
     * @param date1 时间1
     * @param date2 时间2
     * @return 相差天数
     */
    public static int differentDays(Date date1,Date date2){
        int diffDays = 0;
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        int day1 = calendar1.get(Calendar.DAY_OF_YEAR);
        int day2 = calendar2.get(Calendar.DAY_OF_YEAR);
        int year1 = calendar1.get(Calendar.YEAR);
        int year2 = calendar2.get(Calendar.YEAR);

        if (year1 != year2)  //不同年
        {
            int timeDistance = 0;
            for (int i = year1 ; i < year2 ;i++){ //闰年
                if (i%4==0 && i%100!=0||i%400==0){
                    timeDistance += 366;
                }else { // 不是闰年
                    timeDistance += 365;
                }
            }
            diffDays = timeDistance + (day2 - day1);
        }else{// 同年
            diffDays = day2-day1;
        }

        if (diffDays > 0){
            diffDays += 1;
        } else if (diffDays < 0){
            diffDays += 1;
        }

        return diffDays;
    }

    public static int diffDays(Date date1,Date date2){
        int diffDays = 0;
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        int day1 = calendar1.get(Calendar.DAY_OF_YEAR);
        int day2 = calendar2.get(Calendar.DAY_OF_YEAR);
        int year1 = calendar1.get(Calendar.YEAR);
        int year2 = calendar2.get(Calendar.YEAR);

        if (year1 != year2)  //不同年
        {
            int timeDistance = 0;
            for (int i = year1 ; i < year2 ;i++){ //闰年
                if (i%4==0 && i%100!=0||i%400==0){
                    timeDistance += 366;
                }else { // 不是闰年
                    timeDistance += 365;
                }
            }
            diffDays = timeDistance + (day2 - day1);
        }else{// 同年
            diffDays = day2-day1;
        }

        if (diffDays >= 0){
            diffDays += 1;
        }

        return diffDays;
    }

    public static Integer differentMinute(Date startDate,Date endDate){
        // 一天的毫秒数
        Long nd = 1000 * 24 * 60 * 60L;
        // 一小时的毫秒数
        Long nh = 1000 * 60 * 60L;
        // 一分钟的毫秒数
        Long nm = 1000 * 60L;
        // 获得两个时间的毫秒时间差异
        Long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少分钟
        Long min = diff % nd % nh / nm;
        return ConvertUtil.toInt(min);
    }

    public static Integer differentSecond(Date startDate,Date endDate){
        // 一天的毫秒数
        Long nd = 1000 * 24 * 60 * 60L;
        // 一小时的毫秒数
        Long nh = 1000 * 60 * 60L;
        // 一分钟的毫秒数
        Long nm = 1000 * 60L;
        // 获得两个时间的毫秒时间差异
        Long diff = endDate.getTime() - startDate.getTime();
        Long ns = 1000L;
        // 计算差多少秒//输出结果
        Long sec = diff % nd % nh % nm / ns;
        return ConvertUtil.toInt(sec);
    }

    /**
    * <p>Title:获取某年某月有多少天</p>
    * <p>Description:年、月使用0占位</p>
    * @param dyear,dmouth
    * @return int
    */
    public static int getDaysByYearAndMonth(String dyear,String dmouth){
        try {
             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
             Calendar calendar = Calendar.getInstance();
             calendar.setTime(simpleDateFormat.parse(dyear+"/"+dmouth));
             return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//根据年月 获取月份天数
         } catch (ParseException e) {
             LOGGER.log(Level.SEVERE, "getDaysByYearAndMonth exception");
             return 0;
         }
    }

    /**
     * <p>Title:获取当前日期</p>
     * <p>Description:暂无</p>
     * @return String
     */
    public static String getToday(){
        try {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            return String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "getToday exception");
            return null;
        }
    }

       /**
        * <p>Title:根据long类型参数获取某一天的开始时间</p>
        * <p>Description:暂无</p>
        * @param timeStamp 当前时间
        * @return long
        */
    public static Long getStartTimeOfDay(long timeStamp) {
        try {
               Calendar calendar = Calendar.getInstance();
               calendar.setTime(new Date(timeStamp));
               calendar.set(Calendar.HOUR_OF_DAY, 0);
               calendar.set(Calendar.MINUTE, 0);
               calendar.set(Calendar.SECOND, 0);
               calendar.set(Calendar.MILLISECOND, 0);
               return calendar.getTimeInMillis();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "getStartTimeOfDay exception, date:{0}", timeStamp);
            return System.currentTimeMillis();
       }
   }

       /**
        * <p>Title:根据long类型参数获取某一天的结束时间</p>
        * <p>Description:暂无</p>
        * @param timeStamp
        * @return long
        */
       public static long getFirstDayOfWeek(long timeStamp) {
       try {
           Calendar calendar = Calendar.getInstance();
           calendar.setTime(new Date(timeStamp));
           calendar.set(Calendar.HOUR_OF_DAY, 0);
           calendar.set(Calendar.MINUTE, 0);
           calendar.set(Calendar.SECOND, 0);
           calendar.set(Calendar.MILLISECOND, 0);
           calendar.set(Calendar.DAY_OF_WEEK, 2);     
           return calendar.getTimeInMillis();
       } catch (Exception e) {
           LOGGER.log(Level.SEVERE, "getFirstDayOfWeek exception, date:{0}", timeStamp);
           return System.currentTimeMillis();
       }
   }

    /**
     * <p>Title:获取当前月的第一天</p>
     * <p>Description:暂无</p>
     * @return long
     */
    public static long getFirstDayOfThisMonth() {
        try {
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar.getTimeInMillis();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "getFirstDayOfThisMonth exception");
            return System.currentTimeMillis();
        }
   }

    public static Calendar getToDay() {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        instance.set(Calendar.MILLISECOND, 0);
        return instance;
    }

    public static Long getDateTime(String d) {
        SimpleDateFormat df = new SimpleDateFormat(YEAR_MONTH_DAY_TIME_MINUTE_SECONDS);
        Date date = null;
        try {
            date = df.parse(d);
        } catch (ParseException ex) {
            Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (date != null) {
            return date.getTime();
        }
        return 0L;
    }


    /**
     * 在输入日期上增加（+）或减去（-）天数
     *
     * @param timeStamp
     *      输入日期
     * @param iday
     *      要增加或减少的天数
     */
    public static Long addDay(Long timeStamp, int iday) {
        Date date = new Date(timeStamp);
        Calendar cd = Calendar.getInstance();

        cd.setTime(date);

        cd.add(Calendar.DAY_OF_MONTH, iday);

        Date time = cd.getTime();

        return time.getTime();
    }

    /**
     * <p>Title:根据long类型参数生成固定字符串</p>
     * <p>Description:暂无</p>
     * @param timeStamp
     * @param format 可随意设置-例如：yyyyMMdd、yyyy-MM-dd hh:mm、yyyy-MM-dd hh:mm:ss
     * @return String
     */
    public static String getFormatDate(Long timeStamp, String format){
        Date date = new Date(timeStamp);
        return new SimpleDateFormat(format).format(date);
    }


    /**
     * 日期转换：Date类型转换为字符串
     * @param date   日期
     * @param pattern  转换规则
     * @return
     */
    public static String  formatDateToStr(Date date, String pattern) {
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String strDate = sdf.format(date);
        return strDate;
    }

    /**
     * 日期转换：String 类型日期格式 转换成Date类型
     * @param str    String 类型日期
     * @param pattern  转换规则
     * @return
     */
    public static Date formatStrToDate(String str, String pattern) {
        Date date = null;
        if (null != str && !str.trim().equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            try {
                date = sdf.parse(str);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return date;
    }


    /**
     * 比较俩日期大小  Date格式
     * @param dt1
     * @param dt2
     * @return 大于 1，小于 -1，等于 0
     */
    public static int compare_date(Date dt1, Date dt2) {
        try {
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取String类型日期 格式 yyyy-MM-dd
     * @return
     */
    public static String stringDate1(){
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY);
        return sdf.format(new Date());
    }

    /**
     * 获取String类型日期 格式 yyMMddHHmmss
     * @return
     */
    public  String stringDate2(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        return sdf.format(new Date());
    }

    /**
     * 获取String类型日期 格式yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String stringDate3(){
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_TIME_MINUTE_SECONDS);
        return sdf.format(new Date());
    }

    /**
     * 获取String类型日期格式 格式：由传输规则而定
     * 规则一：yyyy-MM-dd
     * 规则二：yyMMddHHmmss
     * 规则三：yyyy-MM-dd HH:mm:ss
     * 规则四：yyyyMMdd
     * 规则五：yyyyMMddHHmmssSS
     * @return
     */
    public static String stringDateRule(String rule){
        SimpleDateFormat sdf = new SimpleDateFormat(rule);
        return sdf.format(new Date());
    }

    /**
     * 日期计算：相加（Date类型） 返回规则 yyyy-MM-dd
     * @return
     */
    public static String calculateDateSum(Date date, int num){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, num);
        String strDate = c.get(Calendar.YEAR) + "-" + intConvertString(c.get(Calendar.MONTH) + 1) + "-" + intConvertString(c.get(Calendar.DATE));
        return strDate;
    }


    /**
     * 日期计算：相减 返回规则 yyyy-MM-dd
     */
    public static String diffDate(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, (0 - num));
        String strDate = c.get(Calendar.YEAR) + "-" + intConvertString(c.get(Calendar.MONTH) + 1) + "-" + intConvertString(c.get(Calendar.DATE));
        return strDate;
    }

    /**
     * 日期计算：相减 返回规则 yyyy-MM-dd
     */
    public String diffDate(String beginDate, int num) {
        Date d = new Date();
        d = new Date(stringToLong(beginDate));
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.DAY_OF_YEAR, num);
        return c.get(Calendar.YEAR) + "-" + intConvertString(c.get(Calendar.MONTH) + 1) + "-" + intConvertString(c.get(Calendar.DATE));
    }


    /**
     * 日期计算：两日期相减 ,返回相差天数
     */
    public static int subtractDate(Date beginDate, Date endDate) {
        return subtractDate(beginDate.getTime(), endDate.getTime());
    }

    /**
     * 日期计算：两日期相减 ,返回相差天数
     */
    public static int subtractDate(long beginDate, long endDate) {
        long subtract = endDate - beginDate;
        int day = (int) (subtract/1000/3600/24);
        return (day + 1);
    }

    /**
     * 日期计算:两日期相减 获得分钟
     */
    public long subtractDateByMinute(long beginDate, long endDate) {
        long diff = endDate - beginDate;
        long result = 0;
        result = diff / 1000 / 60;
        return result;
    }


    /**
     * 获取当前小时
     */
    public String getHour() {
        Calendar c = Calendar.getInstance();
        return intConvertString(c.get(Calendar.HOUR_OF_DAY));
    }


    /**
     * 获取当月中的第一天 返回格式 yyyy-MM-dd
     */
    public String getFirstDateOfCurrentMonth() {
        Calendar c = Calendar.getInstance();
        String date = c.get(Calendar.YEAR) + "-" + intConvertString(1 + c.get(Calendar.MONTH)) + "-01";
        return date;
    }

    /**
     * 获取当月月中的最后一天 返回格式 yyyy-MM-dd
     */
    public String getLastDateOfMonth() {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.DATE, 1);
        c.roll(Calendar.DATE, -1);
        String date = c.get(Calendar.YEAR) + "-" + intConvertString(1 + c.get(Calendar.MONTH)) + "-" + intConvertString(c.get(Calendar.DATE));
        return date;
    }

    /**
     * 获取上月第一天
     * @return
     */
    public static String getPreviousMonthFirst()
    {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(5, 1);
        lastDate.add(2, -1);
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获取上月最后一天
     * @return
     */
    public static String getPreviousMonthEnd()
    {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(2, -1);
        lastDate.set(5, 1);
        lastDate.roll(5, -1);
        str = sdf.format(lastDate.getTime());
        return str;
    }



    /**
     * 获取一周中的第一天 返回格式 yyyy-MM-dd
     */
    public String getFirstDateOfWeek() {
        Calendar c = Calendar.getInstance();
        int dd = c.get(GregorianCalendar.DAY_OF_WEEK);
        if (dd == 1){
            dd = 7;
        }else if (dd > 1){
            dd = dd - 1;
        }
        c.add(Calendar.DATE, 1 - dd);
        String date = c.get(Calendar.YEAR) + "-" + intConvertString(1 + c.get(Calendar.MONTH)) + "-" + intConvertString(c.get(Calendar.DATE));
        return date;
    }


    /**
     * 获取一周中的最后一天
     */
    public String getLastDateOfWeek() {
        Calendar c = Calendar.getInstance();
        int dd = c.get(GregorianCalendar.DAY_OF_WEEK);
        if (dd == 1){
            dd = 7;
        }else if (dd > 1){
            dd = dd - 1;
        }
        c.add(Calendar.DATE, 7 - dd);
        String date = c.get(Calendar.YEAR) + "-" + intConvertString(1 + c.get(Calendar.MONTH)) + "-" + intConvertString(c.get(Calendar.DATE));
        return date;
    }

    /**
     * 获取一周中的第一天
     */
    public String getFirstDateOfWeek(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        int dd = c.get(GregorianCalendar.DAY_OF_WEEK);
        if (dd == 1){
            dd = 7;
        } else if (dd > 1){
            dd = dd - 1;
        }
        c.add(Calendar.DATE, 1 - dd);
        String date = c.get(Calendar.YEAR) + "-" + intConvertString(1 + c.get(Calendar.MONTH)) + "-" + intConvertString(c.get(Calendar.DATE));
        return date;
    }

    /**
     * 获取一周中的最后一天
     */
    public String getLastDateOfWeek(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        int dd = c.get(GregorianCalendar.DAY_OF_WEEK);
        if (dd == 1){
            dd = 7;
        }else if (dd > 1){
            dd = dd - 1;
        }
        c.add(Calendar.DATE, 7 - dd);
        String date = c.get(Calendar.YEAR) + "-" + intConvertString(1 + c.get(Calendar.MONTH)) + "-" + intConvertString(c.get(Calendar.DATE));
        return date;
    }


    /**
     * 获取第二天
     */
    public static String getNextDay(Date date,String pattern) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        /**
         * 把日期往后增加一天.整数往后推,负数往前移动
         */
        calendar.add(Calendar.DATE, 1);
        /**
         * 这个时间就是日期往后推一天的结果
         */
        date = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    /**
     * 获取某个时间段中的日期列表 输入日期为 yyyy-MM-dd 格式
     */
    public List<String> getDatesBetween(String beginDate, String endDate) {
        List<String> dates = new ArrayList<String>();
        int num = subtractDate(stringToLong(beginDate), stringToLong(endDate));
        for (int i = 0; i < num; i++) {
            dates.add(diffDate(beginDate, i));
        }
        return dates;
    }


    /**
     * 字符串日期转换成中文格式日期 输入 2014-07-07 输出 二〇一四年七月七日
     */
    public String dateToCnDate(String date) {
        String result = "";
        String[] cnDate = new String[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String ten = "十";
        String[] dateStr = date.split("-");
        for (int i = 0; i < dateStr.length; i++) {
            for (int j = 0; j < dateStr[i].length(); j++) {
                String charStr = dateStr[i];
                String str = String.valueOf(charStr.charAt(j));
                if (charStr.length() == 2) {
                    if (charStr.equals("10")) {
                        result += ten;
                        break;
                    } else {
                        if (j == 0) {
                            if (charStr.charAt(j) == '1'){
                                result += ten;
                            }else if (charStr.charAt(j) == '0'){
                                result += "";
                            }else{
                                result += cnDate[Integer.parseInt(str)] + ten;
                            }
                        }
                        if (j == 1) {
                            if (charStr.charAt(j) == '0'){
                                result += "";
                            }else{
                                result += cnDate[Integer.parseInt(str)];
                            }
                        }
                    }
                } else {
                    result += cnDate[Integer.parseInt(str)];
                }
            }
            if (i == 0) {
                result += "年";
                continue;
            }
            if (i == 1) {
                result += "月";
                continue;
            }
            if (i == 2) {
                result += "日";
                continue;
            }
        }
        return result;
    }


//    public static String dateDiff(Date startDate, Date endDate) {
//        // 一月的毫秒数
//        long nM = 1000 * 24 * 60 * 60 * 30;
//        // 一天的毫秒数
//        long nd = 1000 * 24 * 60 * 60;
//        // 一小时的毫秒数
//        long nh = 1000 * 60 * 60;
//        // 一分钟的毫秒数
//        long nm = 1000 * 60;
//        long ns = 1000;// 一秒钟的毫秒数long diff;try {
//        // 获得两个时间的毫秒时间差异
//        long diff = endDate.getTime() - startDate.getTime();
//        long month = diff / nM;
//        // 计算差多少天
//        long day = diff / nd;
//        // 计算差多少小时
//        long hour = diff % nd / nh;
//        // 计算差多少分钟
//        long min = diff % nd % nh / nm;
//        // 计算差多少秒//输出结果
//        long sec = diff % nd % nh % nm / ns;
//        if (month != 0){
//            return Math.abs(month) + "月";
//        }
//        if (day != 0){
//            return day + "天";
//        }
//        if (hour != 0){
//            return hour + "小时";
//        }
//        if (min != 0){
//            return min + "分钟";
//        }
//        if (sec != 0){
//            return sec + "秒";
//        }
//        return "1秒";
//    }

    public static String dateDiff(Date startDate, Date endDate) {
        return dateDiff(startDate.getTime(),endDate.getTime());
    }

    /**
     * 获取两个时间的时间查 如1天2小时30分钟
     */
    public static String dateDiff(long startLong, long endLong) {
        String diff = "";
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long _diff = endLong - startLong;
        if (_diff <= 0) {
            diff = "0天";
            return diff;
        }
        long ns = 1000;
        // 计算差多少天
        long day = _diff / nd;
        // 计算差多少小时
        long hour = _diff % nd / nh;
        // 计算差多少分钟
        long min = _diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = _diff % nd % nh % nm / ns;
        if (day > 0) {
            diff += day + "天 ";
        }
        if (hour > 0) {
            diff += hour + "时 ";
        }
        if (min > 0) {
            diff += min + "分 ";
        }
        if (sec > 0) {
            diff += min + "秒";
        }
        return diff;
    }


    /**
     * 类型转换：int转String
     * @param n
     * @return
     */
    private static String intConvertString(int n) {
        if (n < 10){
            return "0" + n;
        }else{
            return "" + n;
        }
    }

    /**
     * 日期转换 String 转long
     * @param someDate
     * @return
     */
    public long stringToLong(String someDate) {
        String format = YEAR_MONTH_DAY;
        if (someDate.length() > 10) {
            format = YEAR_MONTH_DAY_TIME_MINUTE_SECONDS;
        }
        SimpleDateFormat dd = new SimpleDateFormat(format);
        try {
            return dd.parse(someDate).getTime();
        } catch (Exception e) {
            return System.currentTimeMillis();
        }
    }


    public static Date parse(String param) {
        Date date = null;
        if (param == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY_TIME_MINUTE_SECONDS);
            try {
                date = sdf.parse(param);
            } catch (ParseException ex) {
            }
            return date;
        }
    }

    /**
     * 获取日期月份区间
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        //格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    /**
     * 日期转换 long  转换为 yyyy-MM-dd HH:mm:ss
     * @param time
     * @return
     */
    public static String longToDate(long time,String str){

        Date date = new Date(time);

        SimpleDateFormat sd = new SimpleDateFormat(str);

        return sd.format(date);

    }


    /**
     * 日期转换
     * @param date   要转换的日期
     * @param beforeConversionRule 转换前的规则
     * @param endConversionRule    转换后的规则
     * @return
     * @throws ParseException
     */
    public static String strToDateFormat(String date,String beforeConversionRule,String endConversionRule)  {
        SimpleDateFormat formatter = new SimpleDateFormat(beforeConversionRule);
        formatter.setLenient(false);
        Date newDate= null;
        try {
            newDate = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        formatter = new SimpleDateFormat(endConversionRule);
        return formatter.format(newDate);
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static Date getDateDiff(Date date,Integer diff){
        Long leaseStartLong = addDay(date.getTime(), diff);
        String leaseStartTimeStr = getFormatDate(leaseStartLong, DateUtil.YEAR_MONTH_DAY_TIME_MINUTE_SECONDS);
        return parse(leaseStartTimeStr);
    }


    /**
     * <h2>日期静态内部类</h2>
     */
    public static class Format{

        // ==格式到年==
        /**
         * 日期格式:年   例如：2018，2019
         */
        public static final String YEAR = "yyyy";


        // ==格式到年月== UN:特殊
        /**
         * 日期格式:年 月  例如：2018-09，2019-09
         */
        public static final String YEAR_MONTH = "yyyy-MM";

        /**
         * 日期格式:年 月  例如：201809，201909
         */
        public static final String YEAR_MONTH_UN = "yyyyMM";


        // ==格式到年月日== UN:特殊 POINT:点 FORMAT:格式化
        /**
         * 日期格式:年 月 日  例如：2018-09-23，2019-09-23
         */
        public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";

        /**
         * 日期格式:年 月 日  例如：20180923，20190923
         */
        public static final String YEAR_MONTH_DAY_UN = "yyyyMMdd";

        /**
         * 日期格式:年 月 日  例如：2018.09.23，2019.09.23
         */
        public static final String YEAR_MONTH_DAY_POINT = "yyyy.MM.dd";

        /**
         * 日期格式:年 月 日  例如：2018年09月23日，2019年09月23日
         */
        public static final String YEAR_MONTH_DAY_FORMAT = "yyyy年MM月dd日";


        // ==格式到年月日 时分== UN:特殊
        /**
         * 日期格式:年 月 日 时 分 秒  例如：2018-09-23 15:40，2019-09-23 15:40
         */
        public static final String YEAR_MONTH_DAY_TIME_MINUTE = "yyyy-MM-dd HH:mm";

        /**
         * 日期格式:年 月 日 时 分 秒  例如：20180923 15:40，20190923 15:40
         */
        public static final String YEAR_MONTH_DAY_TIME_MINUTE_UN = "yyyyMMdd HH:mm";


        // ==格式到年月日 时分秒== UN:特殊
        /**
         * 日期格式:年 月 日 时 分 秒   例如：2018-09-23 15:40:45，2019-09-23 15:40:45
         */
        public static final String YEAR_MONTH_DAY_TIME_MINUTE_SECONDS = "yyyy-MM-dd HH:mm:ss";

        /**
         * 日期格式:年 月 日 时 分 秒   例如：20180923154045，20190923154045
         */
        public static final String YEAR_MONTH_DAY_TIME_MINUTE_SECONDS_UN = "yyyyMMddHHmmss";


        // ==特殊格式==
        /**
         * 日期格式:时 分 秒
         */
        public static final String TIME_MINUTE_SECONDS = "HH:mm:ss";

        /**
         * 日期格式:分 秒
         */
        public static final String MINUTE_SECONDS = "mm:ss";
    }

}
